package com.bvblogic.android;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import android.util.Log;

public class User {
	
	public static final String  KEY_TOKEN			= "token";
	public static final String  KEY_ID				= "id";
	public static final String  KEY_NAME			= "name";	
	public static final String  KEY_LASTNAME		= "lastname";	
	public static final String  KEY_EMAIL			= "email";
	public static final String  KEY_GENDER			= "gender";	
	public static final String  KEY_BIRTHDATE	 	= "birth_date";
	public static final String  KEY_MARITALSTATUS	= "marital_status";
	public static final String  KEY_PHONE			= "phone";
	public static final String  KEY_PHOTO_URL		= "photo";	
	public static final String  KEY_FRIENDSID		= "friends";
	public static final String  KEY_PWD				= "pwd";
	
	public static final String  KEY_DUIN			= "duin";
	public static final String  KEY_REZ				= "rez";
	public static final String  KEY_PROMOS			= "promos";
	public static final String  KEY_PLACES			= "places";

	public String id;
	public String name;
	public String last_name;
	public String email;
	public String gender;
	public String birth_date;
	public String marital_status;
	public String phone;
	public String photo_url;
//	public ArrayList<String> friends_list;
	
	public static User initFromJSON(JSONObject jsonObject) {
		if (jsonObject == null || jsonObject.length() == 0)
			return null;

		User user = new User();

		// data = {
		// birthdate = "0000-00-00";
		// "company_name" = <null>;
		// "company_position" = <null>;
		// "company_type" = IT;
		// firstname = some name;
		// "friendship_status" = 0;
		// id = 439;
		// "last_update" = "1970-01-01 3:00:00";
		// lastname = some name;
		// latitude = <null>;
		// longitude = <null>;
		// nickname = some nick;
		// "photo_url" = <null>;
		// "photo_url_128" = "some url";
		// "photo_url_64" = "some url";
		// sex = m;
		// };

		String value;

		value = jsonObject.optString(KEY_ID);
		if (value != null && !(value.equals(null)))
			user.id = value;

		value = jsonObject.optString(KEY_NAME);
		if (value != null && !(value.equals("null")))
			user.name = value;

		value = jsonObject.optString(KEY_LASTNAME);
		if (value != null && !(value.equals("null")))
			user.last_name = value;

		value = jsonObject.optString(KEY_GENDER);
		if (value != null && !(value.equals("null")))
			user.gender = value;

		value = jsonObject.optString(KEY_PHOTO_URL);
		Log.d("VALUE AFTER optSTring_PHOTO", value);
		if (value != null && !(value.equals("null")))
			user.photo_url = value;

		value = jsonObject.optString(KEY_BIRTHDATE);
		if (value != null && !value.equals("null") && !value.equals("0000-00-00")) {
			try {
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date date = df.parse(value);
				df = new SimpleDateFormat("dd-MM-yyyy");
				user.birth_date = df.format(date).toString();
			} catch (ParseException pe) {
			}
		}
		
		value = jsonObject.optString(KEY_MARITALSTATUS);
		if (value != null && !(value.equals("null")))
			user.marital_status = value;		
		
		value = jsonObject.optString(KEY_PHONE);
		if (value != null && !(value.equals("null")))
			user.phone = value;	
		
		value = jsonObject.optString(KEY_EMAIL);
		if (value != null && !(value.equals("null")))
			user.email = value;	
		
		
//		JSONArray friendsListValue = jsonObject.optJSONArray(KEY_FRIENDSID);


//		value = jsonObject.optString(KEY_LAST_UPDATE);
//		if (value != null && !(value.equals("null")))
//			user.last_update = value;

		value = null;

		return user;
	}	

}
